package com.ea.service.impl;

import com.ea.common.exception.ServiceException;
import com.ea.common.util.IdWorker;
import com.ea.domain.entity.OrderFormInfo;
import com.ea.domain.entity.OrderFormLog;
import com.ea.mapper.OrderFormLogMapper;
import com.ea.service.IOrderLogService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className IOrderLogServiceImpl
 * @date 2022-12-14 00:24:48
 */
@Service
public class IOrderLogServiceImpl implements IOrderLogService {

    @Autowired
    private OrderFormLogMapper logMapper;

    @Override
    public void insertOrderLog(String msg, Long orderId, Long logId, String operator) {
        logMapper.insertLogByLogId(msg, orderId, logId, operator);
    }

    @Override
    public void insertOrderLog(OrderFormLog log) {
        logMapper.insertOrderLog(log);
    }

    @Override
    public void insertOrderLog(String msg, OrderFormInfo info, String operator) {
        if (info != null) {
            insertOrderLog(msg, info.getOrderId(), info.getRelId(), operator);
        }
    }

    @Override
    public void insertBatchOrderLog(List<OrderFormLog> logList) {
        if (CollectionUtils.isNotEmpty(logList)) {
            logMapper.insertBatchLog(logList);
        }
    }

    @Override
    public void insertBatchOrderLog(String msg, List<Long> orderIdList, String operator) {
        List<OrderFormLog> logList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderIdList)) {
            orderIdList.forEach(order -> {
                OrderFormLog formLog = new OrderFormLog();
                formLog.setLogId(IdWorker.nextId());
                formLog.setLogMsg(msg);
                formLog.setOrderId(order);
                formLog.setOperator(operator);
                logList.add(formLog);
            });
        }
        if (CollectionUtils.isNotEmpty(logList)) {
            insertBatchOrderLog(logList);
        }
    }

    @Override
    public List<OrderFormLog> selectOrderLogByOrderId(Long orderId) {
        if (orderId == null) {
            throw new ServiceException("500" , "订单日志查看失败");
        }
        return logMapper.selectLogByOrderId(orderId);
    }

}
